package cn.com.qingz.modules.user.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

import cn.com.qingz.modules.system.service.impl.GeneralServiceImpl;
import cn.com.qingz.modules.user.mapper.ModelMapper;
import cn.com.qingz.modules.user.model.Model;
import cn.com.qingz.modules.user.service.ModelService;
import cn.com.qingz.modules.user.vo.ModelSearchCondition;

@Service
public class ModelServiceImpl extends GeneralServiceImpl<Model> implements ModelService {

    @Autowired
    private ModelMapper modelMapper;

    public void doAddOrUpdate(Model model) {
        if (model.getModelId() != null) {
            mapper.deleteByPrimaryKey(model.getModelId());
        }
        mapper.insert(model);
    }

    public PageList<Model> queryForPage(Object condition, int page, int limit) {
        return modelMapper.queryForPage((ModelSearchCondition) condition, new PageBounds(page, limit));
    }

    public List<Model> getModelsByCategoryId(String categroyId) {
        return modelMapper.getModelByCategoryId(categroyId);
    }

    @Override
    public List<Model> getModelByListId(String idlist) {
        List<String> list = Arrays.asList(idlist.split(","));
        if(CollectionUtils.isEmpty(list)) return new ArrayList<>();
        return modelMapper.getByIdList(list);
    }

    public String batchSave(List<Model> models) {
        return null;
    }

}
